#include "tls_x509.h"

int kcert_parse(const void *pkt, unsigned char *pubkey, int cert_size) {
    struct x509_certificate *cert;
    cert = x509_cert_parse(pkt, cert_size);

	if (IS_ERR(cert))
		return PTR_ERR(cert);
	
    printk("=====================================\n");
	printk("verified: %d\n", cert->self_signed);
	printk("Cert Issuer: %s\n", cert->issuer);
	printk("Cert Subject: %s\n", cert->subject);
	printk("Cert Key Algo: %s\n", cert->pub->pkey_algo);
	printk("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
	printk("Cert pubkey length:%d\n", cert->pub->keylen);
    // print_hex_dump(KERN_CONT, "cert pubkey:", DUMP_PREFIX_OFFSET, 16, 1, cert->pub->key, cert->pub->keylen, false);

    if (!strcmp(cert->pub->pkey_algo, "rsa")) {
        printk("type is rsa !\n");
    } else {
        printk("type is ecdsas !\n");
    }
    memcpy(pubkey, cert->pub->key, cert->pub->keylen);
    // print_hex_dump(KERN_CONT, "tbs:", DUMP_PREFIX_OFFSET, 16, 1, cert->tbs, cert->tbs_size, false);
    printk("=====================================\n");
    x509_free_certificate(cert);
    return 0;
}
